import SysUtil from './utils/SysUtil'
import { defaultHt, mainFontColor } from './common'

const { ipcRenderer } = require('electron')
// let coresHeight = 0

const contentStyle = () => {
  return {
    color: '#b9c4d1'
  }
}

/**
 * 获取cpu信息
 */
const cpuInfo = async () => {
  const data = await SysUtil.info()
  return new Promise((resolve) => {
    resolve(data)
  })
}

let isShow = false

/**
 * cpu负载信息及核心负载信息
 */
const cpuLoadInfo = async () => {
  const data = await SysUtil.cpuLoadInfo()
  if (!isShow) {
    setHt({ cores: data.cpus.length })
    ipcRenderer.send('showWin')
    isShow = true
  }
  data.cpus.forEach((data) => {
    data.load = data.load !== 100 ? data.load.toFixed(2) : 100
  })
  const cLoad = Number(data.currentLoad.toFixed(2))
  window.topInfo[0] = cLoad !== 100 ? cLoad : 100
  return new Promise((resolve) => {
    resolve(data)
  })
}

const cpuWinHt = async () => {
  const data = await SysUtil.cpuLoadInfo()
  const contHt = calHt(data.cpus.length)
  return new Promise((resolve) => {
    resolve(contHt + defaultHt)
  })
}

/**
 *
 * @returns {Promise<unknown>}
 */
const cpuInfos = async () => {
  const cpu = await cpuInfo()
  const loadInfo = await cpuLoadInfo()
  const data = {
    cpu,
    loadInfo
  }
  return new Promise((resolve) => {
    resolve(data)
  })
}

/**
 * 设置高度
 */
const setHt = (data) => {
  if (data && data.cores) {
    const contHt = calHt(data.cores)
    ipcRenderer.send('winHtChange', contHt + defaultHt)
  }
}

const calHt = (cores) => {
  return cores / 2 * 25 + 220
}

const getCpuOption = (t, data) => {
  let sys = 0
  let user = 0
  let idle = 0
  if (data && data.currentLoadSystem) {
    sys = data.currentLoadSystem.toFixed(2)
    user = data.currentLoadUser.toFixed(2)
    idle = data.currentLoadIdle.toFixed(2)
  }

  return {
    tooltip: {
      trigger: 'item'
    },
    legend: {
      orient: 'vertical',
      left: 'left',
      top: 'center',
      textStyle: {
        fontSize: 12,
        color: mainFontColor
      }
    },
    color: ['#73C0DE', '#5470C6', '#FAC858'],
    series: [
      {
        type: 'pie',
        data: [
          {
            value: sys,
            name: `${t('sys')} ${sys + '%'}`,
            label: {
              color: mainFontColor
            }
          },
          {
            value: user,
            name: `${t('user')} ${user + '%'}`,
            label: {
              color: mainFontColor
            }
          },
          {
            value: idle,
            name: `${t('idle')} ${idle + '%'}`,
            label: {
              color: mainFontColor
            }
          }
        ],
        center: ['85%', '50%'],
        label: {
          show: false,
          position: 'center'
        },
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: 'rgba(0, 0, 0, 0.5)'
          }
        }
      }
    ]
  }
}

export { cpuInfo, contentStyle, cpuLoadInfo, setHt, cpuWinHt, getCpuOption }
